shodanfandomcom_ru-20200215-history
Модуль:Util
Functions --- -- UTF-8 Reference: -- 0xxxxxxx - 1 byte UTF-8 codepoint (ASCII character) -- 110yyyxx - First byte of a 2 byte UTF-8 codepoint -- 1110yyyy - First byte of a 3 byte UTF-8 codepoint -- 11110zzz - First byte of a 4 byte UTF-8 codepoint -- 10xxxxxx - Inner byte of a multi-byte UTF-8 codepoint local function chsize(char) if not char then return 0 elseif char > 240 then return 4 elseif char > 225 then return 3 elseif char > 192 then return 2 else return 1 end end -- This function can return a substring of a UTF-8 string, properly handling -- UTF-8 codepoints. Rather than taking a start index and optionally an end -- index, it takes the string, the starting character, and the number of -- characters to select from the string. local function utf8sub(str, startChar, numChars) local startIndex = 1 while startChar > 1 do local char = string.byte(str, startIndex) startIndex = startIndex + chsize(char) startChar = startChar - 1 end local currentIndex = startIndex while numChars > 0 and currentIndex <= #str do local char = string.byte(str, currentIndex) currentIndex = currentIndex + chsize(char) numChars = numChars -1 end return str:sub(startIndex, currentIndex - 1) end --- function explode(div,str) -- credit: http://richard.warburton.it if (div '') then return false end local pos,arr = 0,{} -- for each divider found for st,sp in function() return string.find(str,div,pos,true) end do table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider pos = sp + 1 -- Jump past current divider end table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider return arr end function tablelength(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end --- Body --- local p = {} function p.dialog(frame) data = ' ' frame = (frame:getParent() or frame) if (frame.args1:gsub("%s*",'') ~= "") then data = data .. ' ' .. (frame.args1 or "| Заголовок Диалога, пжлст") :match( "^%s*(.-)%s*$" ) .. ' ' end for i = 2, tablelength(frame.args), 2 do data = data .. ' ' .. (frame.argsi or "| Говорящий | Фраза, пжлст") :match( "^%s*(.-)%s*$" ) .. ': ' .. (frame.argsi+1 or ""):match( "^%s*(.-)%s*$" ) .. ' ' end return data .. ' ' end function p.links(frame) linklist = {} link = {} title = {} qwert = explode(",", (frame.args1 or "")) --qwert = mw.text.split("Вивек, eeee", ",") if qwert ~= "" then data = "" for i = 1, tablelength(qwert) do linklist = explode("@", qwerti) qwerti = qwerti:gsub("%[%""):gsub("%%]", "") linki = (linklist1 or qwerti):match("^%s*(.-)%s*$") titlei = (linklist2 or qwerti):match("^%s*(.-)%s*$") end ---- -- if mw.title.new(link).exists true then -- data = '[.. qwert[1 -- :match("^%s*(.-)%s*$") .. ']]' -- else -- data = (title1 or link1) -- :match("^%s*(.-)%s*$") -- end for i = 1, tablelength(link) do if i > 1 then data = data .. ', ' end if mw.title.new(linki).exists true then data = data .. '[.. link[i if titlei ~= "" then data = data .. "|" .. titlei end data = data .. ']]' elseif string.match(linki, "^https?:%/%/") then data = data .. '.. link[i if titlei ~= "" then data = data .. " " .. titlei end data = data .. ']' else data = data .. (titlei or linki):match("^%s*(.-)%s*$") end end data = data:gsub("%[%[%[",':gsub("%%]%]",']') end return data end function p.gender(frame) qwert = (frame.args1 or "") qwert = utf8sub(qwert,1,1):lower() if (qwert "f" or qwert "ж" or qwert "Ж") then data = '♀' elseif (qwert "m" or qwert "м" or qwert "М") then data = '♂' else data = qwert end return data end function p.dragonscript(frame) frame = (frame:getParent() or frame) tag = {} tag1 = '<' .. (frame.args3 or "span") .. ' class="Dragonic-font" style="font-size: ' .. (frame.args2 or "20px") .. '; line-height: 1.1; word-spacing: .5em;">' tag2 = '' string = (frame.args1 or "") :match( "^%s*(.-)%s*$" ) -- Trim space ?? :lower() -- text lowercase! :gsub("\n" ,' ') -- prevent paragraph division -- dict :gsub('aa','1'):gsub('ah','4') :gsub('ei','2'):gsub('ey','9') :gsub('ii','3'):gsub('ir','7') :gsub('oo','8'):gsub('uu','5') :gsub('ur','6'):gsub('_' ,' ') return tag1 .. string .. tag2 end function p.cat(frame) qwert = mw.text.split((frame.args1 or "") :gsub("%[%, ""):gsub("%[%[category:" , "") :gsub("%[%[Категория:", ""):gsub("%[%[категория:", "") :gsub("%%]", ""), ",") -- explode(",", (frame.args1 or ""):gsub("%[%""):gsub("%%]", "")) if qwert ~= "" then data = '[.. qwert[1 :match( "^%s*(.-)%s*$" ) .. ']]' for i = 2, tablelength(qwert) do data = data .. ', [.. qwert[i :match( "^%s*(.-)%s*$" ) .. ']]' end -- data = data:gsub("%[%[%[",'-- :gsub("%%]%]",']') end return data end function p.nobr(frame) frame = frame:getParent() -- tag = {} tag1 = '<' .. (frame.args2 or "span") .. ' style="white-space: nowrap">' tag2 = '' -- data = frame.args1 :gsub("\n", ' ') :gsub(" ", ' ') :gsub("-", '‑') return tag1 .. data .. tag2 end return p